/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wikikii.revealbanner.slice;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wikikii.revealbanner.MyAdapter;
import com.wikikii.revealbanner.ResourceTable;
import com.wikikii.revealbanner.bean.BannerListBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerViewAbilitySlice
 *
 * @since 2021-06-09
 */
public class RecyclerViewAbilitySlice extends AbilitySlice {
    private XRecyclerView mRecyclerView;
    private MyAdapter<BannerListBean> mAdapter;
    private List<BannerListBean> feedList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_recycler_view);

        initView();
    }

    private void initView() {
        mRecyclerView = (XRecyclerView) findComponentById(ResourceTable.Id_abt_linear_xrecyclerview);
        mRecyclerView.setLongClickable(false);
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (mRecyclerView != null) {
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        e.fillInStackTrace();
                    }
                    mRecyclerView.refreshComplete();
                    mAdapter.notifyDataChanged();
                }
            }

            @Override
            public void onLoadMore() {
            }
        });

        feedList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            BannerListBean bannerListBean = new BannerListBean();
            feedList.add(bannerListBean);
        }
        mAdapter = new MyAdapter<>(getContext(), feedList, ResourceTable.Layout_item_banner);
        mAdapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {
            }
        });
        mRecyclerView.setItemProvider(mAdapter);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
